<template>
  <div class="app-container">
    <div>
      <el-button type="warning" icon="el-icon-edit" @click="toUpdate()">修改</el-button>
      <el-button class="el-icon-circle-plus" type="warning" @click="toAdd()">添加</el-button>
      <el-button class="el-icon-delete" type="warning" @click="todel()">删除</el-button>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="menuId"
      border
      default-expand-all
      :tree-props="{children: 'menuList'}"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        prop="menuId"
        label="菜单ID"
        type=""
      />

      <el-table-column
        prop="name"
        label="菜单名称"
      />
      <el-table-column
        prop="parentName"
        label="上级菜单"
      />
      <el-table-column
        prop="icon"
        label="图标"
      />
      <!--        <template v-slot="scope">-->
      <!--          <icon :class="scope.row.icon"></icon>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column
        prop="type"
        label="类型"
      >
        <template scope="scope">

          <span v-if="scope.row.type==0">
            <el-button type="primary" size="mini">目录</el-button>
          </span>
          <span v-if="scope.row.type==1">
            <el-button type="success" size="mini">菜单</el-button>
          </span>
          <span v-if="scope.row.type==2">
            <el-button type="warning" size="mini">按钮</el-button>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderNum"
        label="排序号"
      />
      <el-table-column
        prop="url"
        label="菜单url"
      />
      <el-table-column
        prop="perms"
        label="授权标识"
      />
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      multipleSelection: []
    }
  },
  created() {
    this.menuList()
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    toUpdate() {
      // 判断修改只能选择一条数据
      if (this.multipleSelection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择要修改的内容',
          type: 'warning'
        })
        return
      } else if (this.multipleSelection.length > 1) {
        this.$message({
          showClose: true,
          message: '只能选择一条数据',
          type: 'warning'
        })
        return
      }
      // 修改  跳页面传参数
      var menuId = this.multipleSelection[0].menuId
      this.$router.push({
        path: '/sys/menuupdate',
        query: {
          menuId: menuId
        }
      })
    },
    toAdd() {
      this.$router.push({
        path: '/sys/menuadd',
        query: {
          menuId: 0
        }
      })
    },
    todel() {
      var menuId = this.multipleSelection.map(row => row.menuId).join(',')

      this.axios.get('http://localhost:8091/menu/delete', { params: { menuId: menuId }}).then(res => {
        if (confirm('确定删除已选数据吗')) {
          if (res.data.code === '200') {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: 'success'
            })
            this.menuList()
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: 'error'
            })
          }
        }
      })
    },
    menuList() {
      this.axios.get('http://localhost:8091/menu/list').then(res => {
        // alert(JSON.stringify(res.data))
        this.tableData = res.data.obj
      })
    }
  }
}
</script>

